package com.hl.toolkit.server.util;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


public class UploadUtil {
	public static final String FILE_PATH = "/userUpload/"; 
	
	public static String upload(MultipartFile file, HttpServletRequest request, String path) throws Exception {
		if (file == null) {
			return "";
		}
		String filename = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
		Random r = new Random();
		filename = r.nextInt(20) + filename;

		String realPath = request.getSession().getServletContext().getRealPath("userImage");
		String nowFile = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		realPath = realPath + "/" + path + "/" + nowFile;// 真实地址

		filename = filename + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
				file.getOriginalFilename().length());
	/*	FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, filename));*/
		return path + "/" + nowFile + "/" + filename;
	}
	
	
	public static String uploadImg(MultipartFile file, HttpServletRequest request) throws Exception{
			String realPath = request.getSession().getServletContext().getRealPath("");
		    String filename = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
		    String nowFile = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		    filename = filename + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
					file.getOriginalFilename().length());
		    String filePath=realPath+"/userImage/"+nowFile+"/";
	        File tempFile = new File(filePath,filename); 
	        
	        if (!tempFile.getParentFile().exists()) {  
	            tempFile.mkdir();  
	        }  
	        file.transferTo(tempFile);  
	        return filename;  
	}
	/**
	 * 
	 * @param file 文件
	 * @param request  域
	 * @param path   存储的地址文件夹
	 * @param prefix  图片名字前缀
	 * @return
	 * @throws Exception
	 */
	public static String uploadImg1(MultipartFile file, HttpServletRequest request,String path,String prefix) throws Exception{
		String realPath = request.getSession().getServletContext().getRealPath("");
	    //生成年月日时间秒6位随机数名字
		String filename = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+DateUtils.getYzm();
	    //上传的日期路径
	    String nowFile = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	    //最终名字
	    filename =prefix+filename + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
				file.getOriginalFilename().length());
	    String filePath=realPath+"/"+path+"/"+nowFile+"/";
	    synchronized (UploadUtil.class){
	    	FileUtils.writeByteArrayToFile(new File(filePath, filename), file.getBytes());
	    }
        return path+"/"+nowFile+"/"+filename;  
	}
	
	/**
	 * 
	 * @param file 文件
	 * @param request  域
	 * @param path   存储的地址文件夹
	 * @param prefix  图片名字前缀
	 * @return
	 * @throws Exception
	 */
	public static String uploadImg2(MultipartFile file, HttpServletRequest request,String path,String prefix) throws Exception{
		String realPath = request.getSession().getServletContext().getRealPath("");
	    String filename = System.currentTimeMillis()+DateUtils.getYzm();
	    String nowFile = new SimpleDateFormat("yyyy-MM-dd").format(new Date());	    
	    filename =prefix+"_"+filename + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
				file.getOriginalFilename().length());
	    String filePath=realPath+"/"+path+"/"+nowFile+"/";
	    synchronized (UploadUtil.class){
	    	FileUtils.writeByteArrayToFile(new File(filePath, filename), file.getBytes());
	    }
        return path+"/"+nowFile+"/"+filename;  
	}
	
	
	
	/**
	 * 
	 * @param file 文件
	 * @param request  域
	 * @param path   存储的地址文件夹
	 * @param prefix  图片名字前缀
	 * @return
	 * @throws Exception
	 */
	public static synchronized String uploadImg3(MultipartFile file, HttpServletRequest request,String path,Integer prefix) throws Exception{
		String realPath = request.getSession().getServletContext().getRealPath("");
	    String filename = System.currentTimeMillis()+DateUtils.getYzm();
	    String nowFile = new SimpleDateFormat("yyyy-MM-dd").format(new Date());	    
	    filename =prefix+"_"+filename + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
				file.getOriginalFilename().length());
	    String filePath=realPath+"/"+path+"/"+nowFile+"/";
	    
	    	try{
	    	FileUtils.writeByteArrayToFile(new File(filePath, filename), file.getBytes());
	    	}catch(IOException e){
	    		e.printStackTrace();
	    	}
	    
        return path+"/"+nowFile+"/"+filename;  
	}
	
	public static synchronized List<String> uploadImg4(List<MultipartFile> files, HttpServletRequest request,String path,Integer prefix) throws Exception{
		List<String> fileList=new ArrayList<String>();
		for(MultipartFile file:files ){
		String realPath = request.getSession().getServletContext().getRealPath("");
	    String filename = System.currentTimeMillis()+DateUtils.getYzm();
	    String nowFile = new SimpleDateFormat("yyyy-MM-dd").format(new Date());	    
	    filename =prefix+"_"+filename + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
				file.getOriginalFilename().length());
	    String filePath=realPath+"/"+path+"/"+nowFile+"/";
	
	    	try{
	    	FileUtils.writeByteArrayToFile(new File(filePath, filename), file.getBytes());
	    	}catch(IOException e){
	    		e.printStackTrace();
	    	}
	        String filePath2=path+"/"+nowFile+"/"+filename;
	        	    fileList.add(filePath2);
		}
        return fileList;  
	}
	
	
	
	  public static File getFile(String fileName) {  
	        return new File(FILE_PATH, fileName);  
	  }  
	  
	  
	  public static String uploadImg5(MultipartFile file, HttpServletRequest request,String path,String prefix) throws Exception{
			String realPath = request.getSession().getServletContext().getRealPath("");
		    String filename = "印度";
		    String nowFile = new SimpleDateFormat("yyyy-MM-dd").format(new Date());	    
		    filename =prefix+"_"+filename + file.getOriginalFilename();
		    String filePath=realPath+"/"+path+"/"+nowFile+"/";
		    synchronized (UploadUtil.class){
		    	String newName=new String(filename.getBytes("UTF-8"),"UTF-8");
		    	FileUtils.writeByteArrayToFile(new File(filePath, newName), file.getBytes());
		    }
	        return path+"/"+nowFile+"/"+filename;  
		}
	  
	  
	 
}
